new Vue({
	el: '#app',
	data: function () {
		return {
			host: window.location.protocol+'//'+window.location.host,
            communityId:'',
            alertList:[],
            showPopup:false,
            remark:'',
            bstap:true,
            requestHeaders:'',
            showLoading:true,
            status:'',
            fileList:[],
            previewImages:[],
            fileListIUpload:[],
		}
	},
	created() {
        var that=this;
        var localStorageCommunityId=JSON.parse(localStorage.getItem('hc_currentCommunityInfo'))
		if(localStorageCommunityId){
			this.communityId=localStorageCommunityId.communityId?localStorageCommunityId.communityId:this.getUrlParam("communityId");
		}else{
			this.communityId=this.getUrlParam("communityId");
        }
        this.storeId=this.getUrlParam("storeId");
        that.status=this.getUrlParam("status");
        that.requestHeaders=getRequestHeaders('05',this.communityId);//获取请求头
        console.log(30,this.storeId)
        that.loadList();
        // console.log(that.requestHeaders)
    },
    filters: {
		subString: function (value) {
            if(value){
                return value.substring(0,10);
            }else{
                return ''
            }
			
		}
	},
	methods: {
		getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
			var r = window.location.search.substr(1).match(reg); //匹配目标参数
			if (r != null) return unescape(r[2]);
			return null; //返回参数值	  
		},
        //去处理
        handleClick(){
            this.showPopup=true;
            this.remark='';
        },
        goBack(){
            window.history.back();
        },
        //调提交-去处理接口
        clickConfirm(){
            var that=this;
            var arrayId=[]
            for (let index = 0; index < this.fileList.length; index++) {
                const element = that.fileList[index].fileIds;
                arrayId.push(element) 
            }
            var fileEquipmentDetail=arrayId.join(',');
            // console.log(fileEquipmentDetail)
            if(this.remark==''){
                vant.Toast('请输入处理结果');
                return;
            }
            if(this.bstap==false){
                return;
            }          
            var url=this.host+'/app/task.updateTaskNew';
            var params={
                id:this.getUrlParam("id"),
                remark:this.remark,
                bizId:this.getUrlParam("bizId"),
                fileEquipmentDetail:fileEquipmentDetail,
                storeId:this.storeId
            }
            that.bstap=false;
            $.ajax({
				url : url,
				type: 'POST',
				dataType:'json',
                contentType: 'text/plain',
                headers:that.requestHeaders,
                data: JSON.stringify(params),
				success : function (res){
					if(res.code=='0000'){
                        that.bstap=true;
                        that.showPopup=false;
                        vant.Toast.success('处理成功！');
                        window.location.href='/agencyTaskMaintenanceList.html?storeId='+that.storeId+'&communityId='+that.communityId; 
					}else{
                        that.bstap=true;
                        that.showPopup=false;
                        vant.Toast.fail('处理失败！');
					}
				},
				error:function(res){
                    that.bstap=true;
                    that.showPopup=false;
                    vant.Toast.fail('处理失败！');
				}
			})
        },
        loadList(){
            var that=this;
            var url=this.host+'/app/task.listTasksNew';
            var params={
                page:1,
                row:1,
                id:this.getUrlParam("id"),
                storeId:this.storeId
            }
            console.log(120,params)
            $.ajax({
				url : url,
				type: 'POST',
				dataType:'json',
                contentType: 'text/plain',
                headers:that.requestHeaders,
                data: JSON.stringify(params),
				success : function (res){
					if(res.code=='0000'){
                        var dataList=res.data;
                        // console.log(dataList)
                        if(dataList.length>0){
                            that.alertList=dataList.splice(0,1);
                            var fileEquipmentDetail=that.alertList[0].fileEquipmentDetail;
                            var listid=[];
                            if(fileEquipmentDetail){
                                listid=fileEquipmentDetail.split(',');
                                var lg=listid.length
                                if( lg>0){
                                    for (let index = 0; index < lg; index++) {
                                        const element =listid[index];
                                        that.getByfileIdQueryImage(element);    
                                    }
                                }
                            }  
                        }
                        that.showLoading = false;
                        
					}else{
                        that.showLoading=false;
                        vant.Toast(res.meta.message);
					}
				},
				error:function(res){
                    that.showLoading=false;
                    vant.Toast('请求超时！');
				}
			})
        },
        afterRead(file){
            // console.log(file);
            var url='';
            var files=file.file
            if(files.size < 1024 * 1024 * 1){//小于1M不压缩图片
                this.notcompressImg(files,url)
            }else{
                if( files.size > 1024 * 1024 * 5 ){//大于5M提示
                    vant.Toast('图片大小请控制在5MB以内!');
                }else{
                    this.compressImg(files,url)
                }
            }
        },
        // 图片不压缩
		notcompressImg(file,url){
			let that = this;
			var reader = new FileReader();
			reader.readAsDataURL(file);
			reader.onload = function (e) {
				url=this.result.substring(this.result.indexOf(',')+1);
				// console.log("不压缩img："+url)
				var fileFrontUrl='data:image/png;base64,'+url;
				that.getFileUpload(fileFrontUrl,file.name)
			}
		},
        // 图片压缩方法
		compressImg(file,url){
        // 注意这里this作用域的问题
            let that = this;
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function(e) {
                var bdata = this.result;
                // 这里quality的范围是（0-1）
                var quality = 0.1;
                var canvas = document.createElement("canvas");
                var ctx = canvas.getContext("2d");
                var img = new Image();
                img.src = bdata;
                img.onload =function() {
                    var width = img.width;
                    canvas.width = width;
                    canvas.height = width * (img.height / img.width);
                    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
                    var data = canvas.toDataURL("image/jpeg",quality);
                    url=data.substring(data.indexOf(',')+1);
                    // console.log("压缩img："+url)
                    var fileFrontUrl='data:image/jpeg;base64,'+url;
                    that.getFileUpload(fileFrontUrl,file.name)
                }
            };
        }, 
        //图片上传接口
		getFileUpload(file,name) {
            // console.log(name)
			let that=this;
			var filelist=[];
			filelist.push(file);
			var url=this.host+'/app/uploadFiles';
			var params={
				files:filelist,
				relTypeCd:'84005',
                storeId:this.storeId
			}
         
			that.loading=true;
            setInterval(function(){
                that.loading=false;
            },3000)
			$.ajax({
				url : url,
				type: 'POST',
				dataType:'json',
				contentType: 'text/plain',
                headers:that.requestHeaders,
				data: JSON.stringify(params),
				success : function (res){
					// console.log('上传图片',res);
					that.bstapBtn=true;
					if(res.code=='200'){
						var fileIds=res.fileIds[0];
						that.fileList.push({
                            fileIds:fileIds,
                            name:name
                        })
                        console.log(that.fileList)
						that.loading=false;
						return;	
					}else{
						that.loading=false
						that.errorPopup=true;
						that.msg=res.msg
					}
				},
				error:function(res){
					that.loading=false
					that.bstapBtn=true;
					that.errorPopup=true;
					that.msg=res.responseText?res.responseText:'请求超时'
				}
			})
		},
        beforeDelete(elIndex) {
            var name=elIndex.file.name;
            var that = this;
            // console.log(name,that.fileList)
            for (let index = 0; index < that.fileList.length; index++) {
                const element = that.fileList[index];
                if(element.name==name){
                    that.fileList.splice(index, 1)
                }
            }
            return (file, name) => {}
        },
        getByfileIdQueryImage(fileIds){
            // console.log(fileIds)
            var that=this;
            //var url=this.host+'/callComponent/download/getFile/file';
            //var imgUrl= url + "?fileId=" + fileIds+'&communityId='+that.requestHeaders.communityId+'&relTypeCd=84005&time=' + new Date();
            var url=this.host+'/callComponent/download/getFile/fileByObjId';
            var imgUrl= url + "?objId=" + fileIds+'&communityId='+that.requestHeaders.communityId+'&fileTypeCd=84005&time=' + new Date();
            that.previewImages.push(imgUrl)
            console.log(imgUrl) 
        },
        //图片预览
        imagePreview(index){
            vant.ImagePreview({
                images: this.previewImages,
                startPosition: index,
            });
        },

    }
});
